package com.hoopchina.util.xml;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;


import android.util.Log;

public class XMLDataNetwork {
	private static final int TIME_OUT=15000;
    public static String DID;
    public static String URL_ABOUTPAGE;
    public static String URL_FEEDBACKPAGE;
    public static String URL_HELPPAGE;
    private static String TAG = "XMLDataNetwork";

    static {
	DID = "";
	URL_HELPPAGE = "http://www.baidu.com/help.xml";
	URL_ABOUTPAGE = "http://www.baidu.com/about.xml";
	URL_FEEDBACKPAGE = "http://www.baidu.com/feedback.xml";
    }

    // TODO
    public static String appendReqUrl(String paramString1, String paramString2) {
	StringBuilder localStringBuilder = new StringBuilder(
		"http://www.baidu.com/api/").append(paramString1)
		.append("?key=").append("69842642483add0a63503306d63f0443")
		.append("&did=");
	String str = DID;
	localStringBuilder = localStringBuilder.append(str);

	return str;
    }

    public static URLConnection getHttpConnection(String paramString)
	    throws SocketTimeoutException, MalformedURLException, IOException {
	Log.v(TAG, "getHttpConnection");

	URL localURL = new URL(paramString);

	HttpURLConnection localHttpURLConnection;

	localHttpURLConnection = (HttpURLConnection) localURL.openConnection();
	localHttpURLConnection.setConnectTimeout(TIME_OUT);
	//int i1 = 5000;
	localHttpURLConnection.setReadTimeout(TIME_OUT);
//	if (MainView.getActivity() != null) {
//	    FlowRateManagement.addFlowRate(MainView.getActivity(),
//		    FlowRateManagement.FLOW_RATE_AUTO,
//		    localHttpURLConnection.getContentLength());
//	} else {
//	    FlowRateManagement.addFlowRate(IndexActivity.getActivity(),
//		    FlowRateManagement.FLOW_RATE_AUTO,
//		    localHttpURLConnection.getContentLength());
//	}
	Log.v(TAG, "connection OK");

	return localHttpURLConnection;

    }

    public static URLConnection getHttpConnection(String paramString1,
	    String paramString2) throws MalformedURLException, IOException {
	URL localURL = new URL(paramString1);

	HttpURLConnection localHttpURLConnection;
	try {
	    localHttpURLConnection = (HttpURLConnection) localURL
		    .openConnection();
	    localHttpURLConnection.setRequestMethod(paramString2);
	    localHttpURLConnection.setConnectTimeout(TIME_OUT);
	    //int i1 = 5000;
	    localHttpURLConnection.setReadTimeout(TIME_OUT);
//	    if (MainView.getActivity() != null) {
//		FlowRateManagement.addFlowRate(MainView.getActivity(),
//			FlowRateManagement.FLOW_RATE_AUTO,
//			localHttpURLConnection.getContentLength());
//	    } else {
//		FlowRateManagement.addFlowRate(IndexActivity.getActivity(),
//			FlowRateManagement.FLOW_RATE_AUTO,
//			localHttpURLConnection.getContentLength());
//	    }
	    return localHttpURLConnection;
	} catch (SocketTimeoutException localSocketTimeoutException) {
	    Log.e(TAG, "error create connection");
	}
	return null;
    }

    public static URLConnection getHttpPostConnection(String paramString1,
	    String paramString2, String paramString3)
	    throws MalformedURLException, IOException {
	URL localURL = new URL(paramString1);
	HttpURLConnection localHttpURLConnection;
	try {
	    localHttpURLConnection = (HttpURLConnection) localURL
		    .openConnection();
	    localHttpURLConnection.setRequestMethod(paramString2);
	    localHttpURLConnection.setConnectTimeout(TIME_OUT);
	    localHttpURLConnection.setReadTimeout(TIME_OUT);
	    localHttpURLConnection.setDoInput(true);
	    localHttpURLConnection.setDoOutput(true);
	    OutputStream localOutputStream = localHttpURLConnection
		    .getOutputStream();
	    byte[] arrayOfByte = paramString3.getBytes("UTF-8");
	    localOutputStream.write(arrayOfByte);
	    localOutputStream.close();
//	    if (MainView.getActivity() != null) {
//		FlowRateManagement.addFlowRate(MainView.getActivity(),
//			FlowRateManagement.FLOW_RATE_AUTO,
//			localHttpURLConnection.getContentLength());
//	    } else {
//		FlowRateManagement.addFlowRate(IndexActivity.getActivity(),
//			FlowRateManagement.FLOW_RATE_AUTO,
//			localHttpURLConnection.getContentLength());
//	    }
	    return localHttpURLConnection;
	} catch (SocketTimeoutException localSocketTimeoutException) {
	    Log.e(TAG, "error create post connection");
	}
	return null;
    }
}
